﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cow.Module;

namespace Cow.Handler
{
    public class ExportViewModelHandler : ExportFromStringBuilder
    {
        public override string SuffixName
        {
            get { return "Model.cs"; }
        }

        protected override string TemplatePath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "/Template/Entity.txt"; }
        }

        protected override List<String> GetExportContent(Design design)
        {
            StringBuilder sb = new StringBuilder();
            // 添加命名空间
            sb.AppendLine("namespace " + design.NameSpace);
            sb.AppendLine("{");
            // 添加类名
            sb.AppendLine("public partial class " + design.ModuleName + ":BaseEntityModel");
            sb.AppendLine("{");
            // 添加字段
            foreach (var definition in design.DefinitionInfo)
            {
                //由于基类中已经定义了id字段，因此对id字段不做处理
                //如果不是布局字段也不处理
                if (definition.Property.ToLower().Equals("id") ||
                    !definition.IsLayout)
                {
                    continue;
                }
                //获取C#类型
                DataType dataType = design.DataTypeInfo.FirstOrDefault(item => item.Name.Equals(definition.DataType));
                String type = TypeHelper.GetStandardType(dataType.Csharp, definition.IsNull);
                //添加验证
                if (definition.IsRequired)
                {
                    sb.AppendLine("[Required]");
                }
                // 添加字段
                sb.AppendLine(String.Format("public {0} {1} {{ get; set; }}", type, definition.Property));
            }
            sb.AppendLine("}");
            sb.AppendLine("}");
            List<String> valueList = new List<string>();
            valueList.Add(sb.ToString());
            return valueList;
        }
    }
}
